package com.freesun.shop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freesun.shop.entity.Prod;
import com.freesun.shop.service.ProdService;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/prod/prod")
public class ProdController {

    private static Logger log= LoggerFactory.getLogger(ProdController.class);

    @Reference(check = false)
    private ProdService prodService;

    @ApiOperation("商品的分页查询")
    @GetMapping("/page")
    public ResponseEntity<IPage<Prod>> pageProd(Prod prod, Page<Prod> page){

        IPage<Prod> iPage=  prodService.findByPage(prod,page);
        return ResponseEntity.ok((iPage));
    }
    @ApiOperation("产品的批量删除")
    @DeleteMapping
    public ResponseEntity<Void> batchDeleteProd(@RequestBody List<Long> ids){
        log.info("批量删除商品,{}",ids);
        prodService.bathDelete(ids);
        return ResponseEntity.ok().build();

    }

    @ApiOperation("商品的新增")
    @PostMapping
    public ResponseEntity<Void> saveProd(@RequestBody Prod prod) {
        prodService.save(prod);
        return ResponseEntity.ok().build();
    }
    @ApiOperation("查询一个商品")
    @GetMapping("/info/{id}")
    public ResponseEntity<Prod> findById(@PathVariable Long id) {
        Prod prod = prodService.getById(id);
        return ResponseEntity.ok(prod);
    }
}
